package edu.ncsu.csc.ip.client.run;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import edu.ncsu.csc.ip.client.PeerClient;
import edu.ncsu.csc.ip.client.protocol.ProtocolClientImpl;
import edu.ncsu.csc.ip.common.Log;

public class RunClient {

	public static void main(String[] args) throws IOException {

		PeerClient peerClient = null;
		BufferedReader stdIn = new BufferedReader(new InputStreamReader(
				System.in));
		String userCommand;
		boolean firstTime = true;
		System.out.println("Please enter <RS IP Address>:<RS Port> <RFC PORT> <RFC Folder LOCATION>");
		System.out.print("Command$");
		while ((userCommand = stdIn.readLine()) != null
				&& !userCommand.equalsIgnoreCase("exit")) {
			if (peerClient == null) {
				try {

					String[] attributes = userCommand.split(" ");
					String[] rsAtts = attributes[0].split(":");
					peerClient = new PeerClient(rsAtts[0].trim(),
							Integer.parseInt(rsAtts[1]),
							Integer.parseInt(attributes[1].trim()), attributes[2].trim());
					
				} catch (Exception e) {
					Log.println("Please enter <RS IP Address>:<RS Port> <RFC PORT> <RFC Folder LOCATION>");
					Log.println("Example: 10.18.1.123:65423 65450 ./rfc/");
					Log.println("Example: 10.18.1.123:65423 65450 C:/rfc/");
				}
			} else if ( userCommand.trim().equalsIgnoreCase("help")) {
				firstTime = false;
				Log.println("Available commands:");
				Log.println("HELP");
				Log.println("REGISTER");
				Log.println("LEAVE");
				Log.println("PQUERY");
				Log.println("KEEPALIVE");
				Log.println("GETRFC-INDEX");
				Log.println("GETRFC-FILE <RFC Number> Example: GETRFC-FILE 7004");
			} else if (!userCommand.trim().equalsIgnoreCase("")) {
				peerClient.runCommand(userCommand);
			}
			System.out.println("Enter HELP to see the list of all commands.");
			System.out.print("Command$");
		}

		System.exit(0);
	}
}
